package com.sudichina.sudichina.view.activity.orlder;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.CarOrder;
import com.sudichina.sudichina.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyOrlderDetailActivity extends AppCompatActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    //订单号
    @BindView(R.id.tv_amod_orldernumber)
    TextView tvAmodOrldernumber;
    //订单状态
    @BindView(R.id.tv_amod_orlderstate)
    TextView tvAmodOrlderstate;
    //订单起始时间和结束时间
    @BindView(R.id.tv_amod_fromandto_time)
    TextView tvAmodFromandtoTime;
    //车牌
    @BindView(R.id.tv_amod_chepai)
    TextView tvAmodChepai;
    //北京亦庄物流园 - 天津港口
    @BindView(R.id.tv_amod_fromandto_adress)
    TextView tvAmodFromandtoAdress;
    //货物名称: B12机床
    @BindView(R.id.tv_amod_goods_name)
    TextView tvAmodGoodsName;
    //货物类型: 机床
    @BindView(R.id.tv_amod_goods_type)
    TextView tvAmodGoodsType;
    //货物数量: 200 台
    @BindView(R.id.tv_amod_goods_number)
    TextView tvAmodGoodsNumber;
    //发货人: 赵先生
    @BindView(R.id.tv_amod_goods_fahuoren)
    TextView tvAmodGoodsFahuoren;
    //联系提货人
    @BindView(R.id.bt_myorlder_datail_call_consignee)
    Button btMyorlderDatailCallConsignee;
    //提货地址: 北京市亦庄经济技术开发区亦庄物流园B区12号
    @BindView(R.id.tv_amod_tohuo_adress)
    TextView tvAmodTohuoAdress;
    //收货人: 李先生
    @BindView(R.id.tv_amod_goods_shouhuoren)
    TextView tvAmodGoodsShouhuoren;
    //联系收货人
    @BindView(R.id.bt_amod_call_shouhuoren)
    Button btAmodCallShouhuoren;
    //收获地址: 天津港口18区24号
    @BindView(R.id.bt_amod_shouhuoren_adress)
    TextView btAmodShouhuorenAdress;
    //￥ 8000.00
    @BindView(R.id.tv_amod_yunfei)
    TextView tvAmodYunfei;
    //    +门到门送货费
    @BindView(R.id.tv_amod_way)
    TextView tvAmodWay;
    //￥ 500.00
    @BindView(R.id.tv_amod_way_yunfei)
    TextView tvAmodWayYunfei;
    @BindView(R.id.tv_amod_jiesuan_jine)
    TextView tvAmodJiesuanJine;
    @BindView(R.id.tv_amod_qiangdan_time)
    TextView tvAmodQiangdanTime;
    @BindView(R.id.bt_myorlder_datail_delete)
    Button btMyorlderDatailDelete;
    @BindView(R.id.bt_myorlder_datail_weiquan)
    Button btMyorlderDatailWeiquan;
    @BindView(R.id.activity_my_orlder_detail)
    LinearLayout activityMyOrlderDetail;
    private CarOrder.ContentBean older;
    //订单集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_orlder_detail);
        ButterKnife.bind(this);

        tvTitle.setText("订单详情");
        Intent intent = getIntent();
        older = (CarOrder.ContentBean) intent.getSerializableExtra("older");
        System.out.println("订单详情界面:" + older.toString());

        initdatas();


    }

    private void initdatas() {
        if (older != null) {
            //设置数据

            //订单号
            tvAmodOrldernumber.setText("订单号:" + older.getOrderNo());
            //订单状态
            String tradeStatus = older.getTradeStatus();
            switch (tradeStatus) {
                case Contains.WEIFUKUAN:
                    //未付款
                    tvAmodOrlderstate.setText("未付款");
                    break;

                case Contains.HUOZHUDAISHOULI:
//货主待受理
                    tvAmodOrlderstate.setText("待受理");

                    break;

                case Contains.CHEZHUDAISHOULI:
//车主待受理
                    tvAmodOrlderstate.setText("待受理");

                    break;


                case Contains.DAITIHUO:
                    //待提货
                    tvAmodOrlderstate.setText("待提货");

                    break;
                case Contains.YIJIAOHUODAIQUEREN:
                    //车主待待确认收货
                    tvAmodOrlderstate.setText("提货中");

                    break;
                case Contains.YITIHUODAIQUEREN:
                    //车主已提货待货主确认
                    tvAmodOrlderstate.setText("已提货");

                    break;


                case Contains.YUNSHUZHONG:
                    tvAmodOrlderstate.setText("运输中");
                    break;


                case Contains.SONGDA:
                    tvAmodOrlderstate.setText("送达");
                    break;


                case Contains.DAISHOUKUAN:
                    tvAmodOrlderstate.setText("待收款");

                    break;

                case Contains.WANCHENG:
                    tvAmodOrlderstate.setText("完成");

                    break;


            }

            //订单起始时间和结束时间
            String transStartTime = older.getTransStartTime();//开始时间
            String transEndTime = older.getTransEndTime();//结束时间
            String s = StringUtils.formatDate(transStartTime);
            String s1 = StringUtils.formatDate(transEndTime);
            tvAmodFromandtoTime.setText(s + " - " + s1);
            //车牌
            tvAmodChepai.setText(older.getCarFirstLetter() + older.getCardNumber());
            //北京亦庄物流园 - 天津港口

            tvAmodFromandtoAdress.setText(older.getFromZone() + " —— " + older.getToZone());
            //货物名称: B12机床
            tvAmodGoodsName.setText("货物名称: " + older.getItemName());
            //货物类型: 机床
            tvAmodGoodsType.setText("货物类型: ");
            //货物数量: 200 台
            if (older.getWeight() == null) {
                if (older.getVolume() == null) {
                    tvAmodGoodsNumber.setText("");
                } else {
                    tvAmodGoodsNumber.setText("货物体积: " + older.getVolume() + "方");
                }
            }
            if (older.getVolume() == null) {
                if (older.getWeight() == null) {
                    tvAmodGoodsNumber.setText("");
                } else {
                    tvAmodGoodsNumber.setText("货物种量: " + older.getWeight() + "吨");
                }
            }

            //发货人: 赵先生
            tvAmodGoodsFahuoren.setText("发货人: " + older.getDeliverAddress());
            //联系提货人
//            btMyorlderDatailCallConsignee;  //   pickupFlag
            //提货地址: 北京市亦庄经济技术开发区亦庄物流园B区12号
            tvAmodTohuoAdress.setText("提货地址: " + older.getPickupAddress());
            //收货人: 李先生
            tvAmodGoodsShouhuoren.setText("收货人: " + older.getConsignee());         //联系收货人
            // btAmodCallShouhuoren;   consigneeCon
            //收获地址: 天津港口18区24号
            btAmodShouhuorenAdress.setText("收获地址: " + older.getConsigneeAdd());
            //￥ 8000.00
            tvAmodYunfei.setText(older.getOrderMoney());
            //    +门到门送货费
            // TODO: 2017/3/22 服务类型
            tvAmodWay.setText("+" + older.getDeliverFlag() + "送货费");

            //￥ 500.00
            tvAmodWayYunfei.setText(older.getDeliverMoney() + "元");
            ;
            tvAmodJiesuanJine.setText(older.getTotalMoney());
            String s2 = StringUtils.formatDate(older.getCrtDate());

            tvAmodQiangdanTime.setText(s2);


        }

        /**
         * 下面的状态栏
         */


    }

    @OnClick({R.id.iv_back, R.id.bt_myorlder_datail_call_consignee, R.id.bt_amod_call_shouhuoren, R.id.bt_amod_shouhuoren_adress, R.id.bt_myorlder_datail_delete, R.id.bt_myorlder_datail_weiquan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_amod_call_shouhuoren:
                String phoneNumebr = older.getConsigneeCon();
                showMyDialog(phoneNumebr);
                break;
            case R.id.bt_myorlder_datail_call_consignee:
                String phoneNumebr2 = older.getPickupFlag();
                showMyDialog(phoneNumebr2);
                break;
            case R.id.bt_amod_shouhuoren_adress:
                break;
            case R.id.bt_myorlder_datail_delete:
                break;
            case R.id.bt_myorlder_datail_weiquan:
                break;
        }
    }

    private void showMyDialog(final String phoneNumebr) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(phoneNumebr);
        builder.setNegativeButton("呼叫", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                        + phoneNumebr));

                if (ActivityCompat.checkSelfPermission(MyOrlderDetailActivity.this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(intent);
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

    }


}
